
// pages/user/choose/choose.js
const urls = require('../../../utils/url.js').urls;
const http = require('../../../utils/url.js').http
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types:4,
    titleArray:[],
    title:'',
    titleid: '',
    name:'',
  },
  bindztChange:function(event){
    var that = this;
    var index = event.detail.value;
    var title = that.data.titleArray[index].name;
    var titleid = that.data.titleArray[index].id;
    that.setData({
      title: title,
      titleid: titleid
    })
  },
  fromSubmit:function(event){
    var that = this;

    var types = that.data.types;
    var zdm = ''
    
    if (types == 4) {
       zdm = 'company_id'
      // 单位
    } else if (types == 5) {
      // 部门
      zdm = 'department_id'
    }
    var param = {
      openid: app.globalData.openid, //openid
      zdm: zdm, //字段名
      zdz: that.data.titleid //字段值
    };
    http(urls.getUserUpdate, param, function (res) {
      console.log(res)
      if (res.code == 0) {
        wx.navigateBack({
        })
        wx.showToast({
          title: res.msg,
        })
      }
    })

  }
,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
         types: wx.getStorageSync("user_type"),
      title: wx.getStorageSync("user_name")
     })
    console.log('type:'+wx.getStorageSync("user_type"))
    console.log('types:' + that.data.types)
    wx.showNavigationBarLoading();
    if (that.data.types == 4) {
      that.setData({
        name: '单位:'
      })
      // 单位
      http(urls.getUsercom, null, function (res) {

        that.setData({
          titleArray: res.data
        })
        console.log(that.data.titleArray)
        // 隐藏加载中
        wx.hideNavigationBarLoading();

      });
    } else if (that.data.types == 5) {
      that.setData({
        name: '部门:'
      })
      //部门
      http(urls.getUserdep, null, function (res) {

        console.log(res.data)
        that.setData({
          titleArray: res.data
        })
        console.log(that.data.titleArray)
        // 隐藏加载中
        wx.hideNavigationBarLoading();

      });
    }
   
  
  

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})